package com.yuntianshi.controller;

import com.yuntianshi.common.MessageContant;
import com.yuntianshi.common.PageResult;
import com.yuntianshi.common.QueryPageBean;
import com.yuntianshi.common.Result;
import com.yuntianshi.pojo.User;
import com.yuntianshi.service.UserSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 后台员工设置
 */
@RestController
@RequestMapping("/usersetting")
public class UserSettingController {

    @Autowired
    private UserSettingService userSettingService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userSettingService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

//    添加
    @RequestMapping("/add")
    public Result add(@RequestBody User user){
        try {
            //判断是否重名
            List<User> userList=userSettingService.findAll();
            for (User user1 : userList) {
                if (user1.getUsername().equals(user.getUsername())){
                    return new Result(false,"新增员工失败，员工名称重复");
                }
            }
            userSettingService.add(user);
        }catch (Exception e){
            return new Result(false, MessageContant.ADD_USER_FAIL);
        }
        return new Result(true, MessageContant.ADD_USER_SUCCESS);
    }

//    删除
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            //        获取当前登录用户信息
            org.springframework.security.core.userdetails.User user =
                    (org.springframework.security.core.userdetails.User)
                            SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//            System.out.println(user);
            Integer role_id = userSettingService.getRoleByUsername(user.getUsername());
//            System.out.println(role_id);
            if (role_id!=1){
                return new Result(false,"删除员工失败，无删除员工权限！！");
            }
            if (id.equals(1)){
                return new Result(false,"删除失败，不能删除管理员");
            }
            userSettingService.delete(id);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e){
            return new Result(false, MessageContant.DELETE_USER_FAIL);
        }
        return new Result(true,MessageContant.DELETE_USER_SUCCESS);
    }

//    根据Id查找
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            User user=userSettingService.findById(id);
            return new Result(true,MessageContant.QUERY_USER_SUCCESS,user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageContant.QUERY_USER_FAIL);
        }
    }

//    修改
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user){
        try {
            //        获取当前登录用户信息
            org.springframework.security.core.userdetails.User loginUser =
                    (org.springframework.security.core.userdetails.User)
                            SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            //判断当前登录用户是否为系统管理员
            Integer role_id = userSettingService.getRoleByUsername(loginUser.getUsername());
            System.out.println(role_id);
            if (role_id!=1){
                return new Result(false,"编辑员工失败，无编辑员工权限！！");
            }
            //编辑员工
            userSettingService.edit(user);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e){
            return new Result(false, MessageContant.EDIT_USER_FAIL);
        }
        return new Result(true,MessageContant.EDIT_USER_SUCCESS);
    }


    @RequestMapping("/updatePassword")
    public Result updatePassword(@RequestBody Map map){
        try {
            String oldPassword= (String) map.get("oldPassword");
            String newPassword1= (String) map.get("newPassword1");
            String newPassword2= (String) map.get("newPassword2");
            //        获取当前登录用户信息
            org.springframework.security.core.userdetails.User loginUser =
                    (org.springframework.security.core.userdetails.User)
                            SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//            System.out.println(loginUser.getPassword());
            //根据用户名获取详细信息
            User user=userSettingService.findByUserName(loginUser.getUsername());
            //判断是否为空
            if (ObjectUtils.isEmpty(user)){
                return new Result(false,"获取用户信息失败，请重新登录后重试！！");
            }
//            密码加密对象
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            //判断旧密码是否正确
            if (!bCryptPasswordEncoder.matches(oldPassword,user.getPassword())){
                return new Result(false,"旧密码输入错误，请重试！！！");
            }
            //判断两次输入密码是否相同
            if (!newPassword1.equals(newPassword2)){
                //两次输入密码不相同
                return new Result(false,"两次输入密码不相同！！！");
            }
            //修改密码
            String bC_newPassword = bCryptPasswordEncoder.encode(newPassword1);
            User user1 = new User();
            user1.setPassword(bC_newPassword);
            user1.setId(user.getId());
            userSettingService.updatePassword(user1);
            return new Result(true,"修改密码成功");

        }catch (Exception e){
            return new Result(false,"修改密码失败");
        }
    }

}
